<?php

class AmpIndexHelper extends Helper
{

  const ASC = 'asc';

  const DESC = 'desc';

  public $dataProvider;

  public function link($label, $field)
  {
    $route = $this->request->get('route');
    
    if (preg_match('#\/order:([^\/:]*)(?::(asc|desc))?#', $route, $o)) {
      $found = true;
      
      $_field = $o[1];
      $_dir = isset($o[2]) ? $o[2] : '';
    }
    else {
      $found = false;
      
      $_field = $field;
      $_dir = self::ASC;
    }
    
    if ($_field == $field) {
      $dir = empty($_dir) || $_dir == self::ASC ? self::DESC : self::ASC;
    }
    else {
      $dir = self::ASC;
    }
    
    if ($found && $_field == $field) {
      $icon = $dir == self::ASC ? $this->Html->image('icons/control-double-270-small.png', '', array(
        'style'=>'vertical-align:middle;'
      )) : $this->Html->image('icons/control-double-090-small.png', '', array(
        'style'=>'vertical-align:middle;'
      ));
    }
    else {
      $icon = '';
    }
    
    $route = $found ? str_replace('order:' . $_field . ':' . $_dir, 'order:' . $field . ':' . $dir, $route) : sy_fix_url($route . '/' . 'order:' . $field . ':' . $dir);
    
    $url = $this->response->makeUrl($route);
    
    return new HTMLElement('a', array(
      'href'=>$url
    ), null, $label, '&nbsp;', $icon);
  }

  public function headers($headers, $attribs = null)
  {
    $attribs = ParameterHolder::parseFrom($attribs);
    $attribs->set('tr', ParameterHolder::parseFrom($attribs->get('tr')));
    
    $tr = new HTMLElement('tr', $attribs->get('tr'));
    
    foreach ($headers as $key => $header) {
      if (is_array($header)) {
        $label = $this->link($header[0], $header[1]);
        $attribs = isset($header[2]) ? $header[2] : null;
        $tr->appendInner(new HTMLElement('th', $attribs, null, $label));
      }
      else {
        $tr->appendInner(new HTMLElement('th', null, null, $header));
      }
    }
    
    return $tr;
  }

  public function row($row, $keys, $attribs = null, $builder = null)
  {
    $attribs = ParameterHolder::parseFrom($attribs);
    $attribs->set('td', ParameterHolder::parseFrom($attribs->get('td')));
    
    $tr = new HTMLElement('tr', array(
      'class'=>$this->Cycle->next('default', 'ui-amp-even', 'ui-amp-odd')
    ));
    
    foreach ($keys as $key) {
      if ($key == 'actions') {
        $col = $builder->buildIndexActions($row);
      }
      else {
        $col = $row->get($key);
      }
      
      $attribs->get('td')->set($key, ParameterHolder::parseFrom($attribs->get('td')->get($key)));
      
      $tr->appendInner(new HTMLElement('td', $attribs->get('td')->get($key), null, $col));
    }
    
    return $tr;
  }

  public function tr()
  {
    $tr = new HTMLElement('tr', array(
      'class'=>$this->Cycle->next('default', 'ui-amp-even', 'ui-amp-odd')
    ));
    
    $args = func_get_args();
    foreach ($args as $arg) {
      $tr->appendInner($arg);
    }
    
    return $tr;
  }

  public function table($data, $keys, $headers, $attribs = null, $builder = null)
  {
    $attribs = ParameterHolder::parseFrom($attribs);
    $attribs->set('td', ParameterHolder::parseFrom($attribs->get('td')));
    
    $table = new HTMLElement('table', array('class'=>'ui-amp-table'));
    $table->appendInner($this->headers($headers, $attribs));
    
    foreach ($data as $row) {
      $table->appendInner($this->row($row, $keys, $attribs, $builder));
    }
    
    return $table;
  }

  public function resolveValue($value, $params = null)
  {
    if (! empty($params) && ParameterHolder::parseFrom($params)->get('useDataProvider') === false) {
      return $value;
    }
    
    if (empty($this->dataProvider)) return $value;
    
    if (is_callable($this->dataProvider)) {
      $value = (array) $value;
      return call_user_func_array($this->dataProvider, $value);
    }
    
    if (is_array($this->dataProvider)) return $this->dataProvider[$value];
  }

}

?>